package com.example.springaop2.config;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;

//@ControllerAdvice
//@ResponseBody
@RestControllerAdvice
public class MyExceptionAdvice {

    @ExceptionHandler(Exception.class)
    public HashMap<String,Object> exceptionAdvice(Exception e){
        HashMap<String,Object> map  = new HashMap<>();
        map.put("state",-1);// 负数通常代表异常情况
        map.put("data",null);// 我都发生了异常，程序直接中断运行，哪来的数据给你
        map.put("msg","异常：" + e.getMessage());
        return map;
    }
}
